/**
 * Created by bearzhao on 15-5-5.
 */
$.fn.input = function () {
    var $this = $(this);
    $this.data("index", 0);
    var name = $this.attr("name");
    var getValue = function (value) {
        if (value!==undefined) {
            $this.val(value);
        } else {
            return $.trim($this.val());
        }
    };
    var getDefaultValue = function() {
        return $this.attr("defaultvalue");
    };
    var verification = {
        //必须的(不能为空判断)
        req: function(obj) {
            var value = getValue();
            return { message: "不能为空", result: value !== "" && value.length > 0 };
        },
        //整型
        int: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            var reg = /^\d{1,}$/;
            return { message: "必须是整数", result: reg.test(v) };
        },
        //数字类型
        num: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            var reg = /^\w+$/;
            return { message: "必须是数字类型", result: reg.test(v) };
        },

        //字符串最长
        maxlen: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            var s = parseInt(obj.html());
            var ret = true;
            if (v != undefined && v != null && v.replace(/[^\x00-\xff]/g, '__').length > s) {
                ret = false;
            }
            return { message: "最多不能超过" + s + "个字符", result: ret };
        },
        //至少多少个字符
        minlen: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            var s = parseInt(obj.html());
            var ret = true;
            if (v != undefined && v != null && v.replace(/[^\x00-\xff]/g, '__').length < s) {
                ret = false;
            }
            return { message: "至少" + s + "个字符", result: ret };
        },
        //数字最大值
        maxnum: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            v = parseInt(v);
            var s = parseInt(obj.html());
            var ret = true;
            if (v != undefined && v != null && v != NaN && v > s) {
                ret = false;
            }
            return { message: "最大值不能大于" + s, result: ret };
        },
        //数字最小值
        minnum: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            v = parseInt(v);
            var s = parseInt(obj.html());
            var ret = true;
            if (v != undefined && v != null && v != NaN && v < s) {
                ret = false;
            }
            return { message: "最小值不能低于" + s, result: ret };
        },
        //时间类型
        date: function(obj) {
            var v = getValue();
            if (!v || v == "") return;
            return true;
        },
        com: function (obj) {
            var v = getValue();
            if (!v || v == "") return;
            var c = $.trim($("#"+obj.html()).val());
            var ret = true;
            if (c !== v) {
                ret = false;
            }
            return { message: "不一致", result: ret };
        },
        //停止验证
        stop: function(obj) {
            return { message: "", result: "stop" };
        },
        //邮件类型
        email: function (obj) {
            var v = getValue();
            if (!v || v == "") return;
            var reg = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
            return { message: "格式不对", result: reg.test(getValue()) };
        },
        //电话号码
        phone: function (obj) {
            var v = getValue();
            if (!v || v == "") return;
            var reg = /^(\({0,1}\d{3,4})\){0,1}(-){0,1}(\d{7,8})$/;
            return { message: "电话号码格式不对", result: reg.test(getValue()) };
        },
        //url 格式
        url: function (obj) {
            var v = getValue();
            if (!v || v == "") return;
            var reg = /^((http|Http)(s)?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/;
            return { message: "Url格式不对", result: reg.test(getValue()) };
        },
        http: function () {

        },
        //正则表达式
        reg: function (obj) {
            var v = getValue();
            if (!v || v == "") return;
            var reg = new RegExp(obj.html());
            var message = obj.attr("message") || "格式错误";
            return { message:message, result: reg.test(v) };
        }
    };
    var message = {
        error: function (name, message) {
            var $message = $("message[for='" + name + "']");
            var $war = $("span[for='" + name + "']");
            var label = $message.attr("label");
            var helper = $message.attr("helper");
            $war.attr("class", "error").html(label + message);
        },
        helper: function (name, message) {
            var $message = $("message[for='" + name + "']");
            var $war = $("span[for='" + name + "']");
            var label = $message.attr("label");
            var helper = $message.attr("helper");
            $war.attr("class", "helper").html(helper);
        },
        right: function (name, message) {
            var $message = $("message[for='" + name + "']");
            var $war = $("span[for='" + name + "']");
            $war.attr("class", "correct").html("");
        },
        clear: function (name) {
            var $war = $("span[for='" + name + "']");
            $war.removeAttr("class").html("");
        }
    };
    var doVerification = function () {
        var $verification = $("verification[for=" + name + "]");
        var vers = $verification.children();
        for (var i = 0; i < vers.length; i++) {
            $this.data("result", verification[vers[i].tagName.toLowerCase()]($(vers[i])));
            if (!$this.data("result")) return;
            if (!$this.data("result").result) {
                message.error(name, $this.data("result").message);
                return;
            }
        }
        if ($this.data("result")) {
            if ($this.data("result").result == "stop") {
                message.clear(name);
                return;
            }
            if ($this.data("result").result) {
                message.right(name, $this.data("result").message);
            }else {
                message.helper(name, $this.data("result").message);
            }
        }
    };

    if ($this.data("index") === 0) {
        $this.bind({
            "focusin.check": function () {
                if (getValue() === getDefaultValue()) {
                    getValue("");
                }
                $this.removeData("result");
                message.helper(name);
            },
            "focusout.check": function () {
                message.clear(name);
                doVerification();
                if (getValue() === "") {
                    getValue(getDefaultValue());
                }
            }, "input propertychange": function () {
                message.clear(name);
                doVerification();
            }
        });
        $this.data("index", 1);
    }

    this.doVerification = doVerification;
    this.message = message;
    this.$this = $this;
    //if ($this.data("result")==undefined) {
    //    doVerification();
    //}
    return this;
}
